﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.Specialized;
using System.Deployment.Application;

namespace BadHabit.Factory.Windows {
    /// <summary>
    /// Interaction logic for Splash.xaml
    /// </summary>
    public partial class Splash : Window {

        public Splash() {
            InitializeComponent();
            

        }

        private bool IsFirstRun {
            get {                
               return Properties.Settings.Default.IsFirstRun;

            }
        }

        private void FirstRun() {

            Visibility = Visibility.Hidden;
            Windows.Welcome w = new Welcome();
            w.ShowDialog();
         
            Properties.Settings.Default.IsFirstRun = false;
            Properties.Settings.Default.Save();

            
        }

        private void SetConnectionParameters() {

            NameValueCollection parameters = SingletonEntryPoint.GetQueryStringParameters();
            if (!String.IsNullOrEmpty(parameters["ServerUrl"]) && !parameters["ServerUrl"].Equals("DEFAULT"))
                BadHabit.Factory.Properties.Settings.Default.ProjectWebServiceURL = parameters["ServerUrl"];

            if (!String.IsNullOrEmpty(parameters["adminAccessKey"])) {
                BadHabit.Factory.Properties.Settings.Default.UserMemberId = "FACTORY\\admin";
                BadHabit.Factory.Properties.Settings.Default.UserAccessKey = parameters["adminAccessKey"];
                BadHabit.Factory.Properties.Settings.Default.UseWindowsAuthentication = false;
            }

            Properties.Settings.Default.Save();
            
        }

     
        private void Window_Loaded(object sender, RoutedEventArgs e) {

            bool showMainWindowAtFirstTime = false;

            if (IsFirstRun) {
                showMainWindowAtFirstTime = true;
                FirstRun();                
            }
            
            versionTextBlock.Text = Client.ClientVersion;
            versionTextBlock.UpdateLayout();

            SetConnectionParameters();
            
            Client.StartApplication();

            Close();

            if (!Client.IsConnected)
                return;

            if (showMainWindowAtFirstTime) {
                Client.ShowMainWindow();
                return;
            }
            

            
            
            // if connected and has a projectnumber parameter loads the project            
            NameValueCollection parameters = SingletonEntryPoint.GetQueryStringParameters();
            int projectNumber = -1;
            if (Int32.TryParse(parameters["ProjectNumber"], out projectNumber)) {
                if (projectNumber > 0) {
                    Client.ShowMainWindow(projectNumber);
                    return;
                }
            }

            int taskId = -1;
            if (Int32.TryParse(parameters["TaskId"], out taskId)) {
                if (taskId > 0) {
                    Windows.TaskDetail w = new Windows.TaskDetail(taskId);
                    w.Show();
                    return;
                }
            }
            

            
        }
    }
}
